﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Text.RegularExpressions;

namespace Web.Page
{
    public partial class XSHTGLMIX_Add : System.Web.UI.Page
    {
        protected string op;

        protected void Page_Load(object sender, EventArgs e)
        {
            op = Request.QueryString["op"];

            if (!IsPostBack)
            {
                this.hdId.Value = op;
                this.lblTitle.Text = "合同明细表";
                initValue();
            }
        }

        private void initValue()
        {           
            //产品id
            XSHTMXB_Dao daoMx = new XSHTMXB_Dao();
            List<object> cp = daoMx.QueryCPAll();
            this.ddlkpid.DataSource = cp;
            this.ddlkpid.DataTextField = "MC";
            this.ddlkpid.DataValueField = "ID";
            this.ddlkpid.DataBind();
            this.ddlkpid.Items.Insert(0, new ListItem("请选择", "0"));
        }


        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            string str2 = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
            Regex myRegex2 = new Regex(str2);

            string str3 = "^([0-9]*)$";
            Regex myRegex3 = new Regex(str3);
            if (!myRegex3.IsMatch(this.tbSL.Text.Trim()))
            {
                this.lbResult.Text = "请输入或选择数量";
                return;
            }
            if (!myRegex2.IsMatch(this.tbDJ.Text.Trim()))
            {
                this.lbResult.Text = "请输入或选择单价";
                return;
            }
            XSHTMXB entity = new XSHTMXB()
            {
                ID = Guid.Parse(Guid.NewGuid().ToString()),
                XSHTID = Guid.Parse(Request.QueryString["xshtid"]),
                CPID = Guid.Parse(this.ddlkpid.SelectedValue),
                SL = Convert.ToDouble(this.tbSL.Text.Trim()),
                DJ = Convert.ToDouble(this.tbDJ.Text.Trim())
            };

            string resultMessage = "";
            try
            {
                XSHTMXB_Dao mxdao = new XSHTMXB_Dao();
                int result = 0;
                result = mxdao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.tbDJ.Text = "";
            this.tbSL.Text = "";
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/XSHTGL_Query.aspx?op=" + this.hdId.Value);
        }
    }
}